﻿using H.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace H.Eyes.Remotings
{
    /// <summary>
    /// 图像识别接口远程引用
    /// </summary>
    public sealed class EyeRef : MarshalByRefObject, IEye
    {
        /// <summary>
        /// 内部封装的Eye引用
        /// </summary>
        private readonly IEye _Eye;

        ///<inheritdoc/>
        public IEyeOption DefaultOption => _Eye.DefaultOption;

        /// <summary>
        /// 获取一个驱动
        /// </summary>
        public EyeRef(string pluginName)
        {
            _Eye = (IPlugin.GetObject<IEyeFactory>(pluginName)?.CreateInstance()) ?? throw new TypeLoadException();
        }

        ///<inheritdoc/>
        public IEyeOption CreateOption()
        {
            return _Eye.CreateOption();
        }

        ///<inheritdoc/>
        public ISee FindColor(params RGB[] colors)
        {
            return new SeeRef(_Eye.FindColor(colors));
        }

        ///<inheritdoc/>
        public ISee FindColor(IEyeOption option = null, params RGB[] colors)
        {
            return new SeeRef(_Eye.FindColor(option, colors));
        }

        ///<inheritdoc/>
        public ISee FindPic(params IImgEntity[] imgs)
        {
            return new SeeRef(_Eye.FindPic(imgs));
        }

        ///<inheritdoc/>
        public ISee FindPic(IEyeOption option, params IImgEntity[] imgs)
        {
            return new SeeRef(_Eye.FindPic(option, imgs));
        }

        ///<inheritdoc/>
        public ISee FindPicB(params IImgEntity[] imgs)
        {
            return new SeeRef(_Eye.FindPicB(imgs));
        }

        ///<inheritdoc/>
        public ISee FindPicB(IEyeOption option, params IImgEntity[] imgs)
        {
            return new SeeRef(_Eye.FindPicB(option, imgs));
        }

        ///<inheritdoc/>
        public bool SetDisplay(byte[] display = null)
        {
            return _Eye.SetDisplay(display);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing"></param>
        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                _Eye.Dispose();
            }
        }

        ///<inheritdoc/>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~EyeRef()
        {
            Dispose(false);
        }

    }

}
